using System;
using System.Linq.Expressions;
using NHibernate.Criterion;
using NHibernate.Type;

namespace FwkLight.Domain.Fetching
{
  public enum Sorting
  {
    Asc,
    Desc
  }

  public class SortingPart : ISortingPart
  {
    public SortingPart(string propertyName, Sorting sorting, IFetchingPart fetchingPart)
    {
      PropertyName = propertyName;
      Sorting = sorting;
      FetchingPart = fetchingPart;
    }

    public string PropertyName { get; private set; }
    public Sorting Sorting { get; private set; }
    public IFetchingPart FetchingPart { get; set; }

    public void ApplySortingOn(DetachedCriteria criteria)
    {
      if (Sorting == Sorting.Asc)
        criteria.AddOrder(Order.Asc(FetchingPart.Alias + "." + PropertyName));
      else criteria.AddOrder(Order.Desc(FetchingPart.Alias + "." + PropertyName));
    }
  }
}